﻿using System.Linq;
using System.Collections.Generic;
using System;
using BusinessObjects;
using DataObjects.EntityFramework.ModelMapper;

namespace DataObjects.EntityFramework.Implementation
{
    /// <summary>
    /// Entity Framework implementation of the ICategoryDao interface.
    /// </summary>
    public class EntityCategoryDao : ICategoryDao
    {
        /// <summary>
        /// Gets list of product categories
        /// </summary>
        /// <returns>List of categories.</returns>
        public List<Category> GetCategories()
        {
            using (var context = DataObjectFactory.CreateContext()) 
            {
                var list = new List<Category>();

                var categories = context.CategoryEntities.ToList();
                foreach(var category in categories)
                    list.Add(Mapper.Map(category));

                return list;
            }
        }
        
        /// <summary>
        /// Gets categoryId for a given a product.
        /// </summary>
        /// <param name="productId">The product identifier.</param>
        /// <returns>The categoryId.</returns>
        public int GetCategoryByProduct(int productId)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var category = (context.ProductEntities.Include("Category")
                   .FirstOrDefault(p => p.ProductId == productId).Category);

                return Mapper.Map(category).CategoryId;
            }
        }

        public List<Category> SearchCategories(DateTime DateFrom,DateTime DateTo, int firstPoint, int lastPoint,int supplierId, int trafficId, string sortExpression)
        {
            List<CategoryEntity> categories = null;
            using (var context = DataObjectFactory.CreateContext())
            {               
                var query = context.CategoryEntities.AsQueryable();
                if(DateTo.ToString("mm/dd/yyyy") == "01/01/1990")
                {
                    if (supplierId == 0 && trafficId == 0)
                    {
                        query = query.Where(c => c.Date >= DateFrom && c.FirstPoint == firstPoint && c.LastPoint == lastPoint);
                    } else if (supplierId != 0 && trafficId == 0)
                    {
                        query = query.Where(c => c.Date >= DateFrom && c.FirstPoint == firstPoint && c.LastPoint == lastPoint && c.SupplierId == supplierId);
                    }
                    else if (supplierId != 0 && trafficId != 0)
                    {
                    query = query.Where(c => c.Date >= DateFrom && c.FirstPoint == firstPoint && c.LastPoint == lastPoint && c.TrafficId == trafficId);
                    }
                }
                else 
                {
                    if (supplierId == 0 && trafficId == 0)
                    {
                        query = query.Where(c => c.Date >= DateFrom && c.Date <= DateTo && c.FirstPoint == firstPoint && c.LastPoint == lastPoint);
                    }
                    else if (supplierId != 0 && trafficId == 0)
                    {
                        query = query.Where(c => c.Date >= DateFrom && c.Date <= DateTo && c.FirstPoint == firstPoint && c.LastPoint == lastPoint && c.SupplierId == supplierId);
                    }
                    else if (supplierId != 0 && trafficId != 0)
                    {
                        query = query.Where(c => c.Date >= DateFrom && c.Date <= DateTo && c.FirstPoint == firstPoint && c.LastPoint == lastPoint && c.TrafficId == trafficId);
                    }
                }

                categories = query.ToList();
                return categories.Select(c => Mapper.Map(c)).ToList();
            }
        }
        public Category GetCategory(int categoryID)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                return Mapper.Map(context.CategoryEntities.SingleOrDefault(s => (s.CategoryId == categoryID)));
            }
        }

        public void InsertCategory(Category category)
        {
            var entity = Mapper.Map(category);
            entity.EndTime = DateTime.Parse("01/01/1990");
            using (var context = DataObjectFactory.CreateContext())
            {
                context.CategoryEntities.AddObject(entity);
                context.SaveChanges();

                // update business object with new version and id
                category.CategoryId = entity.CategoryId;

                //country.Version = Convert.ToBase64String(entity.Version);
                //return category.CategoryId;
            }
        }

        public void UpdateCategory(Category category)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var entity = context.CategoryEntities.Where(c => c.CategoryId == category.CategoryId).SingleOrDefault();

                entity.CategoryId = category.CategoryId;
                entity.TrafficId = category.TrafficId;
                entity.Date = category.Date;
                entity.Description = category.Description;
                entity.FirstPoint = category.FirstPoint;
                entity.LastPoint = category.LastPoint;
                entity.StartTime = category.StartTime;
                entity.EndTime = category.EndTime;
                entity.Status = category.Status;
                
                context.CategoryEntities.ApplyCurrentValues(entity);
                context.SaveChanges();

                // Update business object with new version

            }
        }

        public void DeleteCategory(Category category)
        {
            using (var context = DataObjectFactory.CreateContext())
            {
                var entity = context.ProductEntities.Where(c => c.CategoryId == category.CategoryId).SingleOrDefault();
                context.ProductEntities.DeleteObject(entity);
                context.SaveChanges();
            }
        }

        
    }
}
